
wx.ready(function () {
	var imgSrc = []; //图片路径
	var imgDeal= [];//处理点
	var imgFile = []; //文件流
	var imgFile1 = []; //文件流
	var imgName = []; //图片名字
	var imgBox = '#imgBox';
	var imgBox1 = '#imgBox1';
	// 5 图片接口
	  // 5.1 拍照、本地选图
	  var images = {
	    localId: [],
	    serverId: []
	  };

	// 二维码扫码
	document.querySelector('#scanQRCode').onclick = function () {
		wx.scanQRCode({
			needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
			scanType: ["qrCode","barCode"], // 可以指定扫二维码还是一维码，默认二者都有
			success: function (res) {
				var result = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
				document.getElementById("scan").src=result;
				$("#codeUrl").val(result);
				$("#scanbtnbox").css("display","none")
				$("#scaniframe").css("display","block")


			}
		});
	};

	function scanclick(){
		$("#scanbtnbox").css("display","none")
		$("#scanbtnbox").append('<ul class="table_photo_min" id="scaniframe"><h5>营业执照扫码信息</h5><iframe  style="width: 100%;height:100%;" id="scan" src="http://222.76.242.84:8084/xmgsggfw/SsztServlet?flag=sszt&action=viewSsztMobile&qyid=2c97f1942c9b741d012ca0a90a5910ca&pritype=06&sfsszt=1&sfjyyc=2&qymc="></iframe></ul>');

	}

	//隐患点图片上传
	document.querySelector('#chooseImage1').onclick = function () {
		var localIds = "";
		wx.chooseImage({
			//count: 9,  默认9
			sizeType:'compressed',
			success: function (res) {
				images.localId = res.localIds;
				//localId = images.localId;
				imgSrc = images.localId;
				addNewContent1(imgBox);
				syncUpload(imgSrc);
			}
		});
	};



	//隐患点上传图片到服务器
	function syncUpload(localIds){
		var localId = localIds.pop();
		wx.uploadImage({
			localId: localId,
			isShowProgressTips: 1,
			sizeType:'compressed',
			success: function (res) {
				var serverId = res.serverId; // 返回图片的服务器端ID
				imgFile.push(serverId);
				//其他对serverId做处理的代码
				if(localIds.length == 0) {
					$("#danUrls").val(imgFile);
				}
				if (localIds.length > 0) {
					syncUpload(localIds);
				}
			}
		});
	}

	//处理点上传图片到服务器
	function syncUpload1(localIds){
		var localId = localIds.pop();

		wx.uploadImage({
			localId: localId,
			isShowProgressTips: 1,
			sizeType:'compressed',
			success: function (res) {
				var serverId = res.serverId; // 返回图片的服务器端ID
				imgFile1.push(serverId);
				//其他对serverId做处理的代码
				if(localIds.length == 0) {
					$("#dealUrls").val(imgFile1);
				}
				if (localIds.length > 0) {
					syncUpload1(localIds);
				}
			}
		});
	}

	//隐患点图片展示
	function addNewContent1(obj) {
		$(obj).html("");
		for(var a = 0; a < imgSrc.length; a++) {
			var oldBox = $(obj).html();
			$(obj).html(oldBox + '<div class="imgContainer"><img  src=' + imgSrc[a] + ' onclick="imgDisplay(this)"></div>');
		}
	}

	//处理点图片展示
	function addNewContent2(obj) {
		$(obj).html("");
		for(var a = 0; a < imgSrc.length; a++) {
			var oldBox = $(obj).html();
			$(obj).html(oldBox + '<div class="imgContainer"><img  src=' + imgSrc[a] + ' onclick="imgDisplay(this)"></div>');
		}
	}


	function uploadImg(localId){
		wx.uploadImage({
			localId:localId,
			success: function (res) {
				$('#media_id').val(res.serverId);
				//  document.forms[0].submit();
			},
			fail: function (res) {
				alert(JSON.stringify(res));
			}
		});
	}

	//隐患点图片上传
	function uploadMoreImg(localId){
		var i = 0, length = localId.length;
		images.serverId = [];
		function upload() {
			wx.uploadImage({
				localId: localId[i],
				success: function (res) {
					i++;
					//alert('已上传：' + i + '/' + length);
					images.serverId.push(res.serverId);
					if (i < length) {
						upload();
					}
				},
				fail: function (res) {
					alert(JSON.stringify(res));
				}

			});
		}
		upload();
	}







	  // 5.2 图片预览
	  document.querySelector('#previewImage').onclick = function () {
	    wx.previewImage({
	      current:  $('#table').attr("src"),
	      urls: [
	        'http://img3.douban.com/view/photo/photo/public/p2152117150.jpg',
	        'http://img5.douban.com/view/photo/photo/public/p1353993776.jpg',
	        'http://img3.douban.com/view/photo/photo/public/p2152134700.jpg'
	      ]
	    });
	  };

	  // 5.3 上传图片
	  document.querySelector('#uploadImage').onclick = function () {
	    if (images.localId.length == 0) {
	      alert('请先使用 chooseImage 接口选择图片');
	      return;
	    }
	    var i = 0, length = images.localId.length;
	    images.serverId = [];
	    function upload() {
	      wx.uploadImage({
	        localId: images.localId[i],
	        success: function (res) {
	          i++;
	          //alert('已上传：' + i + '/' + length);
	          images.serverId.push(res.serverId);
	          if (i < length) {
	            upload();
	          }
	        },
	        fail: function (res) {
	          alert(JSON.stringify(res));
	        }
	      });
	    }
	    upload();
	  };

	  // 5.4 下载图片
	  document.querySelector('#downloadImage').onclick = function () {
	    if (images.serverId.length === 0) {
	      alert('请先使用 uploadImage 上传图片');
	      return;
	    }
	    var i = 0, length = images.serverId.length;
	    images.localId = [];
	    function download() {
	      wx.downloadImage({
	        serverId: images.serverId[i],
	        success: function (res) {
	          i++;
	          alert('已下载：' + i + '/' + length);
	          images.localId.push(res.localId);
	          if (i < length) {
	            download();
	          }
	        }
	      });
	    }
	    download();
	  };




	  
  });
